package main

import "fmt"

func longestPalindrome(s string) string {
	if len(s) <= 1 {
		return s
	}
	long := 0
	s1 := string(s[0])

	//偶数回文
	for i := 0; i < len(s)-1; i++ {
		if s[i] != s[i+1] {
			continue
		}
		for k := 0; k <= i && k <= len(s)-i-2; k++ {
			if s[i-k] != s[i+k+1] {
				if long < 2*k {
					s1 = s[i-k+1 : i+k+1]
					long = 2 * k
				}
				break
			} else if (i-k == 0 || i+k+1 == len(s)-1) && long < 2*(k+1) {
				s1 = s[i-k : i+k+2]
				long = 2 * (k + 1)
				break
			}
		}
	}

	//基数回文
	for i := 0; i < len(s)-2; i++ {
		if s[i] != s[i+2] {
			continue
		}
		for k := 0; k <= i && k <= len(s)-i-3; k++ {
			if s[i-k] != s[i+k+2] {
				if long < 2*k+1 {
					s1 = s[i-k+1 : i+k+2]
					long = 2*k + 1
				}
				break
			} else if (i-k == 0 || i+k+2 == len(s)-1) && long < 2*(k+1)+1 {
				s1 = s[i-k : i+k+3]
				long = 2*(k+1) + 1
			}
		}

	}
	return s1

}

func main() {
	// 示例整数

	// 打印结果
	//fmt.Println("bb:  ", longestPalindrome("qasdsaq"))
	fmt.Println("bb:  ", longestPalindrome("abcbe"))
}
